package com.ai.pos.server;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ai.pos.client.sales.models.SalesItem;
import com.ai.pos.server.database.MySQLAccess;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SalesItemServlet extends RemoteServiceServlet implements com.ai.pos.client.SalesItemService {
	

	private static final long serialVersionUID = 5155606739307753502L;


	//Will throw exception
	private SalesItem getSalesItem(int _salesItemRecId) throws SQLException
	{	
		String query = "SELECT * FROM sales_item WHERE" + Integer.toString(_salesItemRecId);
		
		try{
			return executeSelect(query).get(0);
		}
		catch(Exception e)
		{
			//TODO: log here.
			return null;
		}
	}
	
	private int saveSalesItem(SalesItem item) throws SQLException
	{	
		
		int newItem = 0;
		
		String insertSQL = "INSERT INTO sales_item (LongName, ButtonOneDesc, ButtonTwoDesc, ItemCategory, Cost, Price) " +
						   "Values('" + item.GetLongName() + "','" + item.getButtonOneName() + "','" + item.getButtonTwoName() + "'," +
						   		     + item.getItemCategory() + "," + item.GetSimpleCostString() + "," + item.getSalePrice() + ")";
		
	    
		MySQLAccess connector = new MySQLAccess();
	    Connection connection = null;
		
		try{
			connection  = connector.getConnection();
		    Statement insert = connection.createStatement();
		    newItem = insert.executeUpdate(insertSQL);
		    
		    //TODO - Invalidate cache that will be used by the menu builder system, etc..
		    
		}
		catch(Exception e)
		{
			//TODO: log here.
			return 0;
		}
		finally{
			if(connection != null)
				connection.close();
		}
		
		return newItem;
	}
	
	
	
	
	
	private ArrayList<SalesItem> executeSelect(String _select)
	{
		String query = _select;
		ResultSet result = null; 
		
		try {
		    MySQLAccess connector = new MySQLAccess();
		    Connection connection = connector.getConnection();
		    
		    Statement select = connection.createStatement();
		    result = select.executeQuery(query);

		} catch(Exception e) {
		        System.err.println("Mysql Statement Error: " + query);
		        e.printStackTrace();
		}
		
		
		ArrayList<SalesItem> items = new ArrayList<SalesItem>();
		
		try {
			while(result.next())
			{
				SalesItem item = new SalesItem();
				item.SetRecId(result.getInt(SalesItem.REC_ID));
				item.SetLongName(result.getString(SalesItem.LONGNAME));
				item.setButtonOneName(result.getString(SalesItem.BUTTON_ONE_NAME));
				
				items.add(item);
			}
		} catch (SQLException e) {
			// Log
			e.printStackTrace();
		}
		
		
		
		
		return items;
		
	}





	@Override
	public String SaveItem(SalesItem item) throws IllegalArgumentException {
		
		String newItemId = "0";
		
		try {
			newItemId = Integer.toString(saveSalesItem(item));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return newItemId;
		//SalesItem item = new SalesItem();
		//item = item.getSalesItemFromJson(itemJson);
		//save
	}


	@Override
	public String getAllItems(String itemJson) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getItem(int itemId) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}

}
